package aleksandar.djuric.statistics;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.swing.SwingUtilities;

import aleksandar.djuric.gui.MainFrame;

public class Statistics extends Thread implements ClipboardOwner{
	
	public static String[] items = {"PAtt", "PCpl", "CAtt", "CCpl", "ImPa",
		"DAtt", "DCpl", "FoAg", "SAtt", "SoG",
		"Trn", "Off", "Ast", "Gol",
		"FoMd", "Int", "TAtt", "TCpl", "KTak",
		"HAtt", "HCpl", "KHdr", "Clr", "FfBW",
		"FfBL", "Sav", "GoAg", "CInt", "YC", "RC"};
	
	private StringBuilder finalStats = new StringBuilder();
	private MainFrame mf = MainFrame.mainFrame;
	private String tmp = null;
	private Statistics statistics = this;
	
	@Override
	public void run() {
		mf.getJFrame().setEnabled(false);
		mf.getDisplay().setText("Fetching match statistics...");
		tmp = null;
		int first = 351541;
		int last = 351672;
		int n = last - first + 1;
		for (int i = 0; i < n; i++) 
		{
			tmp = mf.getFetcher().getMatchStats(i + first);
			finalStats.append(tmp);
			mf.getDisplay().setText("Fetching match statistics for " 
					+ n + " matches...<br>"
					+ "match: " + (i + first) + " / " + last + "<br>"
					+ Math.round((i + 1) * 100. / n) + "%");
		}
		
		tmp = null;
		String[] array = null;
		ArrayList<Footballer> footballers = new ArrayList<Footballer>();
		BufferedReader in = new BufferedReader(new StringReader(finalStats.toString()));
		
		try {
			while((tmp = in.readLine()) != null)
			{
				if(tmp.equals(""))
					continue;
				array = tmp.split("_");
				Footballer footballer = null;
				for (int i = 0; i < footballers.size(); i++) {
					footballer = footballers.get(i);
					if(array[2].equals(footballer.name))
						break;
					else
						footballer = null;
				}
				ArrayList<Integer> list = new ArrayList<Integer>();
				for (int i = 0; i < array.length - 2; i++) {
					try {
						list.add(Integer.parseInt(array[i]));
					} catch (Exception e) {
					}
				}
				if(footballer == null)
				{
					footballer = new Footballer();
					footballer.team = array[0];
					footballer.url = array[1];
					footballer.name = array[2];
					footballer.position = array[3];
					list.toArray(footballer.stats);
					footballers.add(footballer);
				}
				else
				{
//				System.out.println(footballer.name);
					footballer.add(list);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally
		{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		finalStats = new StringBuilder();
		tmp = footballers.size() + 
		" footballers total from " + n + " matches.<br>" +
		"Lines that follow are now in the clipboard, just paste them into excel document.<br><br>";
		finalStats.append(tmp);
		
//		for (int i = 0; i < items.length; i++) {
//			finalStats.append(items[i] + " ");
//		}
		
		for (int i = 0; i < footballers.size(); i++) {
			finalStats.append(footballers.get(i).team + "_");
			finalStats.append("=Hyperlink(\"" + footballers.get(i).url + "\";\"" + footballers.get(i).name + "\")_");
			finalStats.append(footballers.get(i).position + "_");
			for (int j = 0; j < footballers.get(i).stats.length; j++) {
				finalStats.append(footballers.get(i).stats[j]);
				if(j < footballers.get(i).stats.length - 1)
					finalStats.append("_");
			}
			finalStats.append("<br>");
		}
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				mf.getDisplay().setText(finalStats.toString());
				int end = mf.getDisplay().getCaretPosition();
				int start = tmp.length() - 8;
				mf.getDisplay().setCaretPosition(start);
				mf.getDisplay().moveCaretPosition(end);
				mf.getDisplay().requestFocus();
				StringSelection ss = new StringSelection(
						mf.getDisplay().getSelectedText());
				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
						ss, statistics);
				mf.scrollToTheBegining();
			}
		});
		mf.getJFrame().setEnabled(true);
	}

	@Override
	public void lostOwnership(Clipboard arg0, Transferable arg1) {
		// Do nothing.
	}
}
